import { getAuthMenu } from "@/config/api/login";
export default {
  data() {
    return {
      mymenu: []
    };
  },
  methods: {
    async getMenu() {
      let res = await getAuthMenu({});
      console.log(res);
      if (res.data.data.root.code === "") {
        this.$router.push({ path: "/" });
      }
      if (res && res.data.code === "0") {
        const arr = res.data.data.root.sons;
        arr.forEach(item => {
          item.pid = "1";
          item.id = 1 + "-" + item.code;
          if (item.sons && item.sons.length) {
            item.sons.forEach(item2 => {
              item2.pid = item.id;
              item2.id = item.id + "-" + item2.code;
              if (item2.sons && item2.sons.length) {
                item2.sons.forEach(item3 => {
                  item3.pid = item2.id;
                  item3.id = item2.id + "-" + item3.code;
                  if (item3.sons && item3.sons.length) {
                    item3.sons.forEach(item4 => {
                      item4.pid = item3.id;
                      item4.id = item3.id + "-" + item4.code;
                    });
                  }
                });
              }
            });
          }
        });
        this.parseJson(arr);

        const routeObj = JSON.parse(JSON.stringify(arr));
        this.$store.dispatch("generateRoutes", routeObj).then(() => {
          this.$router.addRoutes(this.$store.getters.addRouters); // 动态添加可访问路由表
        });
        console.log(this.mymenu);
        this.$store.dispatch("setMenu", this.mymenu);
      }
    },
    parseJson(arr) {
      // var pp = undefined
      if (arr.length !== 0) {
        var pp = function(arr) {
          for (var i = 0; i < arr.length; i++) {
            arr[i].meta = {};
            arr[i].menuCode = arr[i].keyword;
            arr[i].meta.isShowSideBar = arr[i].showLeftMenu === 1;
            arr[i].isShowSideBar = arr[i].showLeftMenu === 1;
            if (arr[i].sons && arr[i].sons.length !== 0) {
              arr[i].subMenu = arr[i].sons;
              // console.log(arr[i].name); // 这里可以写菜单的样式结构...
              pp(arr[i].sons);
            } else {
              // console.log(arr[i].name); // 这里可以写菜单的样式结构...
              arr[i].meta.isShowSideBar = arr[i].showLeftMenu === 1;
              arr[i].isShowSideBar = arr[i].showLeftMenu === 1;
            }
          }
        };
        pp(arr);
      }
      // console.log("arr");
      // console.log(arr);
      this.mymenu = arr;
    }
  }
};
